package servlet;

import entity.User;
import service.UserService;
import service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@WebServlet("/list")
public class UserListServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 调用service
        UserService userService = new UserServiceImpl();
        List<User> userList =  userService.listUsers();
        req.setAttribute("userList", userList);

        req.setAttribute("key1","req");
        req.getSession().setAttribute("key2","session");
        getServletContext().setAttribute("key3","application");

        req.setAttribute("emptyNull", null);
        req.setAttribute("emptyStr", "123");
        req.setAttribute("emptyArr",new Object[]{});
        ArrayList<String> objects = new ArrayList<>();
        objects.add("lisi");
        req.setAttribute("emptyList",objects);
        req.setAttribute("emptyMap",new HashMap<>());
        req.getRequestDispatcher("user.jsp").forward(req, resp);
    }
}
